/*
 * readline.h
 *
 *  Created on: Jul 28, 2010
 *      Author: Pierre Lindenbaum PhD
 *              plindenbaum@yahoo.fr
 *              http://plindenbaum.blogspot.com
 *              
 */

#ifndef U915_IO_ISTREAMBUF_H_
#define U915_IO_ISTREAMBUF_H_
#include <streambuf>
#include <cstdio>

namespace u915 { namespace io {

/**
* an abstract buffered input from std::streambuf&lt;char&gt;
*/
class istreambuf : public std::streambuf
    {
    protected:
	/** buffer */
	char* buffer;
	/** current size of the buffer */
	unsigned int buffer_size;
	/** buffer capacity */
	unsigned int buffer_capacity;

	void _init(unsigned int capacity)
	    {
	    if(capacity<=0) capacity=1;
	    this->buffer_size=0;
	    this->buffer_capacity=capacity;
	    this->buffer=new char[this->buffer_capacity];

	    setg( &this->buffer[0],
	    &this->buffer[this->buffer_capacity],
	    &this->buffer[this->buffer_capacity]
	    );
	    }


	istreambuf()
	    {
	    _init(BUFSIZ);
	    }

	istreambuf(unsigned int buffer_size)
	    {
	    _init(buffer_size);
	    }

    public:
	virtual ~istreambuf()
	    {
	    if(this->buffer!=NULL)
		{
		delete [] this->buffer;
		this->buffer=NULL;
		}
	    }
    };

}}//namespaces



#endif /* TOKENIZER_H_ */
